<?php
declare (strict_types=1);

namespace app\school\logic\school\admin;

use app\common\helper\util\Tool;
use app\school\model\school\admin\School as SchoolModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
use think\Paginator;

/**
 * 后台管理逻辑层
 */
class School extends SchoolModel
{
    /**
     * 机构列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-12
     *
     * @param array $map
     * @return Paginator [type]                         [description]
     * @throws DbException
     */
    public function getList(array $map = []): Paginator
    {
        $schoolDomain = app(SchoolDomain::class);

        return $this->with(['level', 'logo', 'admin'])
            ->where('id', '<>', 1)
            ->where($map)
            ->order('update_time', 'DESC')
            ->paginate(static::getListRows())
            ->each(function ($i) use ($schoolDomain) {
                $i->mhm_cname = $schoolDomain->getPcSubDomain($i->id);//$this->getSchoolDomain($i->id);
            });
    }

    /**
     * 机构待审核列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-12
     *
     * @param array $map
     * @return Paginator [type]                         [description]
     * @throws DbException
     */
    public function getWaitingList(array $map = []): Paginator
    {
        return $this->with(['apply', 'logo'])->where($map)->order('create_time')->paginate(static::getListRows());
    }

    /**
     * 搜索机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-06
     *
     * @param string $keyword [description]
     * @param int    $limit
     * @param array  $map
     * @return mixed
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function getSearchList(string $keyword = '', int $limit = 12, array $map = [])
    {
        $builder = $this;
        if ($keyword != '') $builder = $this->where(function ($query) use ($keyword) {
            $query->where('school_code|title', 'like', '%' . $keyword . '%');
        });

        return $builder->where($map)->field(['id' => 'value', 'title', 'id', 'from', 'status'])->limit($limit)->order(['update_time' => 'desc'])->select()->hidden(['from', 'status', 'status_text', 'from_text']);
    }

    /**
     * 批量更新数据
     *
     * @param array  $ids
     * @param string $field
     * @param        $value
     */
    public function batchUpdated(array $ids, string $field, $value)
    {
        $ret = SchoolModel::update([$field => $value], [['id', 'in', $ids]]);
        if (!$ret) {
            throw new LogicException('批量操作失败');
        }
    }

    /**
     * 批量删除
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/16
     *
     * @param $ids
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function deleteSchools($ids): void
    {
        $list = $this->whereIn('id', $ids)->select();
        if (!$list->delete()) {
            throw new LogicException('操作失败');
        }
    }

    /**
     * 获取机构信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/6/24
     *
     * @param int $mhmId
     * @return array|Model
     */
    public function getSchoolInfoById(int $mhmId)
    {
        return $this->where('id', $mhmId)->findOrEmpty();
    }

    /**
     * 获取机构域名
     *
     * @param $id
     * @return string
     */
    public function getSchoolDomain($id)
    {
        return 'm' . $id . '.' . Tool::pregDomain(config('eduline.appinfo.pc_host'));
    }

}
